<template>
  <div></div>
</template>
<script>
import {loginPA } from '@/api/PAPublic'
export default {
  data() {
    return {
      userId: '',
      fromType:'',
    };
  },
  beforeCreate() {
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#fff");
  },
  beforeDestroy() {
    document.querySelector("body").setAttribute("style", "");
  },
  created() {
    this.$store.commit("setEnvCode", "qyb");
    this.autoLogin();
  },
  methods: {
    autoLogin() {
      this.userId = this.$route.query.token || this.getUrlCode().token; // userid
      this.fromType = this.$route.query.from || this.getUrlCode().from;
      console.log('获取的token:'+this.userId)
      if (this.userId == null || this.userId === "") {
         this.$router.push("/login");
      } else {
        this.login();
      }
    },
    getUrlCode() {
      // 截取url中的code方法
      var url = location.search;
      this.winUrl = url;
      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
      }
      return theRequest;
    },
    login(){
      if (window.Android) {
                    window.Android.postMessage(JSON.stringify({
                      targetName:"Eqb",
                      actionName:"hiddenNavgationBar",
                      data:{},
                      callback:{
                        success:""
                      }
                }))}else if(window.webkit){
                window.webkit.messageHandlers.EQBNative.postMessage(JSON.stringify({
                    targetName:"Eqb",
                          actionName:"hiddenNavgationBar",
                          data:{},
                          callback:{
                            success:""
                          }
              }))}
        let data = {
            UserId: this.userId
        };
        loginPA(data)
        .then((res)=>{
            if (res.data.UserName){
                console.log("免登陆成功")
                // 修改登陆状态
                this.$store.commit("updateLogin", true);
                // 把token存入store
                this.$store.commit("updateToken", res.data.UserName);
                // 把用户名存入store
                this.$store.commit("updateUserName", res.data.UserName);
                // 把用户名存入store
                this.$store.commit("updateChnName", res.data.ChnName);
                // 保存地址 updateArea
                this.$store.commit("updateArea", res.data.ApplyArea);
                // 保存地址 updateArea
                this.$store.commit("updateCompanySysType", res.data.CompanySysType);
                // 公司code
                this.$store.commit("updateCompanyCode", res.data.CompanyCode);
                // 模块
          this.$store.commit("setIsTempUser", res.data.IsTempUser);
                this.$store
                .dispatch("getRoles", res.data.UserKind)
                .then((data) => {
                    console.log(data);
                })
                .catch((_) => {
                    failure("获取失败");
                });
                setTimeout(() => {
                    this.$router.push(
                        "/index?num=" + Math.ceil(Math.random() * 100000)
                    );
                }, 1500);
            }else{
                this.$router.push({path:'/login',query:{paUserID:this.userId}});
            }
        })
        .catch((err)=>{
            console.log("免登陆失败")
            this.$router.push({path:'/login',query:{paUserID:this.userId}});
        })
    },
  },
};
</script>